package com.SeleniumTestsFirefox;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

public class MakePaymentTestCases extends SeleneseTestBase {

	@Before
	public void setUp() throws Exception {
		WebDriver driver = new FirefoxDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}

	// Created by Steven Chow
	// Scenario 1: Sucessful Payment
	// Test Case 1: Successful Credit Card Payment
	@Test
	public void testMakePaymentSC1TC1() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User3");
		selenium.type("name=password", "pass3");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Very Hot");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "F12B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 150.0"));
		// Choose Points
		selenium.type("name=credit", "150");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
	}

	// Created by Steven Chow
	// Scenario 1: Sucessful Payment
	// Test Case 2: Successful Points Payment
	@Test
	public void testMakePaymentSC1TC2() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User4");
		selenium.type("name=password", "pass4");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Hot");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "F11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 200"));
		// Choose Points
		selenium.type("name=point", "200");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
	}

	// Created by Steven Chow
	// Scenario 1: Sucessful Payment
	// Test Case 3: Successful Combo Payment

	@Test
	public void testMakePaymentSC1TC3() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User5");
		selenium.type("name=password", "pass5");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Pretty");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "E12B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 150.0"));
		// Choose Points
		selenium.type("name=credit", "75");
		selenium.type("name=point", "75");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we made a reservation
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
	}

	
	// Created by Steven Chow
	// Scenario 2: Unsuccessful Payment
	// Test Case 1: Unsuccessful Credit Card payment
	@Test
	public void testMakePaymentSC2TC1() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User6");
		selenium.type("name=password", "pass6");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "E11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		// Don't pay
		assertTrue(selenium.isTextPresent("Price: 200.0"));
		// Choose Credit
		selenium.type("name=credit", "0");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium.isTextPresent("Inadequate Amount"));
		selenium.click("//a[contains(@href, '/Website/SelectFlightURL?username=User6&r1=1001')]");
		selenium.waitForPageToLoad("30000");
		// Repeat for Adding more than the amount
		// Fill in all the stuff
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "E11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=credit", "600");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		// Check if we get an error
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium
				.isTextPresent("Your paid more than the price. Please go back and adjust your payment"));
		selenium.click("//a[contains(@href, '/Website/SelectFlightURL?username=User6&r1=1001')]");
		selenium.waitForPageToLoad("30000");
		// Repeat for an invalid number like a character (no number)
		// Check if we made a reservation
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "E11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=credit", "8Bitch!!");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we got an error!
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium.isTextPresent("Invalid Number"));
	}

	// Created by Steven Chow
	// Scenario 2: Unsuccessful Payment
	// Test Case 2: Unsuccessful Points Payment
	@Test
	public void testMakePaymentSC2TC2() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User7");
		selenium.type("name=password", "pass7");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Leina");
		selenium.type("name=lastName", "Locksterr");
		selenium.type("name=seatID", "A11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		// Don't pay
		assertTrue(selenium.isTextPresent("Price: 200.0"));
		// Choose Credit
		selenium.type("name=point", "0");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium.isTextPresent("Inadequate Amount"));
		selenium.click("//a[contains(@href, '/Website/SelectFlightURL?username=User7&r1=1001')]");
		selenium.waitForPageToLoad("30000");
		// Repeat for Adding more than the amount
		// Fill in all the stuff
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "E11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=point", "600");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		// Check if we get an error
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium
				.isTextPresent("Your paid more than the price. Please go back and adjust your payment"));
		selenium.click("//a[contains(@href, '/Website/SelectFlightURL?username=User7&r1=1001')]");
		selenium.waitForPageToLoad("30000");
		// Repeat for an invalid number like a character (no number)
		// Check if we made a reservation
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "E11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=point", "8Bitch!!");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we got an error!
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium.isTextPresent("Invalid Number"));
	}

	// Created by Steven Chow
	// Scenario 2: Unsuccessful Payment
	// Test Case 3: Unsuccessful Combo payment

	@Test
	public void testMakePaymentSC2TC3() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User7");
		selenium.type("name=password", "pass7");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Leina");
		selenium.type("name=lastName", "Locksterr");
		selenium.type("name=seatID", "A11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		// Don't pay
		assertTrue(selenium.isTextPresent("Price: 200.0"));
		// Choose Credit
		selenium.type("name=point", "0");
		selenium.type("name=credit", "0");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium.isTextPresent("Inadequate Amount"));
		selenium.click("//a[contains(@href, '/Website/SelectFlightURL?username=User7&r1=1001')]");
		selenium.waitForPageToLoad("30000");
		// Repeat for Adding more than the amount
		// Fill in all the stuff
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "A11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=credit", "600");
		selenium.type("name=point", "600");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		// Check if we get an error
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium
				.isTextPresent("Your paid more than the price. Please go back and adjust your payment"));
		selenium.click("//a[contains(@href, '/Website/SelectFlightURL?username=User7&r1=1001')]");
		selenium.waitForPageToLoad("30000");
		// Repeat for an invalid number like a character (no number)
		// Check if we made a reservation
		selenium.type("name=firstName", "Anime");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "A11B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=credit", "8Bitch!!");
		selenium.type("name=point", "8Bitch!!");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we got an error!
		assertEquals("Payment Error", selenium.getTitle());
		assertTrue(selenium.isTextPresent("Invalid Number"));
	}

	// Created by Steven Chow
	// Scenario 3: Cancelled Payment
	// Test Case 1: Go back to home page
	@Test
	public void testMakePaymentSC3TC1() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User3");
		selenium.type("name=password", "pass3");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We in Customer portal?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search Flights
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// 2nd one
		// Make Reservation
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in all the stuff
		selenium.type("name=firstName", "Very Hot");
		selenium.type("name=lastName", "Girl");
		selenium.type("name=seatID", "D12B");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Make the reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are in the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		assertTrue(selenium.isTextPresent("Price: 150.0"));
		selenium.selectWindow("null");
		selenium.click("//a[contains(@href, '/Website/MakePaymentURL?username=User3&r1={$flightID}')]");
		selenium.waitForPageToLoad("30000");
		// Check to see if we go back to the reservation page! NOT THE HTTP
		// ERROR!!!!
		assertEquals("Reservation", selenium.getTitle());
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}

}
